package com.obtk.controllerSGW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.modelSGW.ProStorage;
import com.obtk.service.ProStorageService;
import com.obtk.util.CoreConst;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-04-01 17:21:33
 */
@Controller
@RequestMapping("/proStorage")
public class ProStorageController {
    @Autowired
    private ProStorageService proStorageService;

    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadUsers(ProStorage proStorage, Integer limit, Integer offset){
        System.out.println(proStorage);
        PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
        List<ProStorage> ProStorageList = proStorageService.selectProStorage(proStorage);
        PageInfo<ProStorage> pages = new PageInfo<>(ProStorageList);
        return ResultUtil.table(ProStorageList,pages.getTotal());
    }


    /**新增用户*/
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(ProStorage ProStorageForm){
        String stkProdName = ProStorageForm.getStkProdName();
        ProStorage proStorage = proStorageService.selectByStkProdName(stkProdName);
        if (null != proStorage) {
            return ResultUtil.error("产品已存在已存在");
        }
        int num = proStorageService.register(ProStorageForm);
        if(num > 0){
            return ResultUtil.success("添加用户成功");
        }else {
            return ResultUtil.error("添加用户失败");
        }
    }

//    /**编辑用户详情*/
//    @GetMapping("/edit")
//    public String customerDetail(Model model, String stkId){
//        ProStorage proStorage = proStorageService.selectByStkProdId(stkId);
//        model.addAttribute("proStorage", proStorage);
//        System.out.println(stkId);
//        return "proStorage/update";
//    }

    /**编辑库存表信息*/
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(ProStorage proStorageForm){
        System.out.println(proStorageForm);
        int a = proStorageService.updateByStkProdId(proStorageForm);
        if (a > 0) {
            return ResultUtil.success("更改库存成功！");
        } else {
            return ResultUtil.error("更改失败");
        }
    }

    /**删除用户*/
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String stkId) {
        List<String> proStorageIdsList = Arrays.asList(stkId);
        int a = proStorageService.updateStatusBatch(proStorageIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除用户成功");
        } else {
            return ResultUtil.error("删除用户失败");
        }
    }
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String proStorageIdsStr) {
        String[] stkProdIds = proStorageIdsStr.split(",");
        List<String> proStorageIdsList = Arrays.asList(stkProdIds);
        int a = proStorageService.updateStatusBatch(proStorageIdsList,CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除用户成功");
        } else {
            return ResultUtil.error("删除用户失败");
        }
    }
}
